package com.boot.entity;

import com.boot.service.unSafeUtils;;
import sun.misc.Unsafe;



/**
 * @ClassName : validateInt
 * @Description : validateInt
 * @Author : Xxxxx
 * @Date: 2023-02-11 10:07
 */
public class validateInt {
    private static long valueOffset;
    private static Unsafe unsafe;
    static {
        try {
            unsafe=unSafeUtils.getInstance();
            valueOffset = unsafe.objectFieldOffset(validateInt.class.getDeclaredField("value"));
        } catch (NoSuchFieldException|IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public validateInt(int value){
        this.value=value;
    }
    private volatile int value;

    public validateInt add(int add){
        while (true){


            if (unsafe.compareAndSwapInt(this,valueOffset,value,add+value)) {
                break;
            }
        }
        return this;
    };

    public validateInt dec(int dec){
        while (true){
            if (unsafe.compareAndSwapInt(this,valueOffset,value,value-dec)){
            break;
            }
        }
        return this;
    };

    @Override
    public String toString() {
        return ""+value;
    }
}
